package com.ayou.eggguard.user.service.impl;

import com.ayou.common.utils.Md5Util;
import com.ayou.common.utils.PageBean;
import com.ayou.eggguard.user.mapper.UserMapper;
import com.ayou.eggguard.user.pojo.User;
import com.ayou.eggguard.user.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public void addUser(String username, String password) {

        String md5String = Md5Util.getMD5String(password);

        userMapper.addUser(username,md5String);

    }

    @Override
    public User getUserById(String username) {
        return userMapper.getUserById(username);
    }

    @Override
    public User getUserByUserName(String username) {
        return userMapper.getUserByUserName(username);
    }

    @Override
    public void updateUserInfo(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUserInfo(user);
    }

    @Override
    public void updatePwd(String username,String newPwd) {
        userMapper.updatePwd(username,Md5Util.getMD5String(newPwd));
    }

    @Override
    public PageBean<User> getUserListByPage(Integer pageNum, Integer pageSize, String username) {
        PageBean<User> pb=new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<User> list=userMapper.getUserListByPage(username);
        Page<User> p= (Page<User>) list;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public void userOpen(Integer id) {
        userMapper.userOpen(id);
    }

}
